from typing import Final

from service.models import Event
from users.models import User, Specialization

MAIN_SPEC_COEF: Final[int] = 4
ADDIT_SPEC_COEF: Final[int] = 1
MISSING_SPEC_COEF: Final[int] = -999


def form_user_coefficients(users, specialisations, types):
    def mapper(user: User, specialisation: Specialization):
        if specialisation.specialization in types[user.main_specialization.specialization]:
            return MAIN_SPEC_COEF
        if specialisation.specialization in [types[x] for x in (spec.specialization for spec in user.additional_specializations.all())]:
            return ADDIT_SPEC_COEF
        else:
            return MISSING_SPEC_COEF

    coefficients = {}
    for user in users:
        coefficients[user.id] = [mapper(user, specialisation) for specialisation in specialisations]
    return coefficients


def form_events_coefficients(events, users_coefficients):
    return [users_coefficients[event.user.id] for event in events]
